package oop.main;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

/**
 * 
 *  This class will be used to display the Frame to submit a new employee
 *  It is also the class that is used to create a employee object
 * 
 * @author Alec Mersmann
 * 
 */


public class Employee extends Person {
	
	public int id;
	public String supervisor;
	
	private double salary;
	
	public static void main(String[] arg)
	{
		
		//TODO This is where the frame is created
		
		final JFrame frame = new JFrame("Employee");
		
		MaskFormatter ssnFormat = null;
		try
		{
			ssnFormat = new MaskFormatter("###-##-####");
		} catch (ParseException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		


		
		
		final JTextField txtFirstName = new JTextField();
		final JTextField txtLastName = new JTextField();
		final JTextField txtSsn = new JFormattedTextField(ssnFormat);
		final JTextField txtAge = new JTextField();
		final JTextField txtEmployeeId = new JTextField();
		final JTextField txtSalary = new JTextField();
		final JTextField txtSupervisor = new JTextField();
		
		JLabel lblFirstName = new JLabel("First Name: ");
		JLabel lblLastName = new JLabel("Last Name: ");
		JLabel lblSsn = new JLabel("Social Security Number: ");
		JLabel lblAge = new JLabel("Age: ");
		JLabel lblEmployeeId = new JLabel("Employee ID: ");
		JLabel lblSalary = new JLabel("Salary: ");
		JLabel lblSupervisor = new JLabel("Supervisor: ");
		
		JButton btnSubmit = new JButton("Submit");
		JButton btnCancel = new JButton("Cancel");

		frame.setLayout(new GridLayout(8, 2));
		frame.setSize(500, 500);
		
		
		
		frame.add(lblFirstName);
		frame.add(txtFirstName);
		frame.add(lblLastName);
		frame.add(txtLastName);
		frame.add(lblSsn);
		frame.add(txtSsn);
		frame.add(lblAge);
		frame.add(txtAge);
		frame.add(lblEmployeeId);
		frame.add(txtEmployeeId);
		frame.add(lblSalary);
		frame.add(txtSalary);
		frame.add(lblSupervisor);
		frame.add(txtSupervisor);
		frame.add(btnSubmit);
		frame.add(btnCancel);

		frame.setVisible(true);
		
		
		btnCancel.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e)
			{
			txtSupervisor.setText("");
			txtAge.setText("");
			txtFirstName.setText("");
			txtLastName.setText("");
			txtEmployeeId.setText("");
			txtSsn.setText("");

			frame.setVisible(false);
			
			
			}
		});
		
		btnSubmit.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e)
			{
				
				boolean valid = true;
				
				char[] testNumber;
	
				if(txtFirstName.getText().isEmpty())
				{
					System.out.println("Enter a first name.");
					valid = false;
				}
				if(txtLastName.getText().isEmpty())
				{
					System.out.println("Enter a last name.");
					valid = false;
				}
				
				int age = -1;;
				
				testNumber  = txtAge.getText().toCharArray();
				
				for(int i=0; i<testNumber.length; i++){
					if(Character.isLetter(testNumber[i]))
					{
						valid = false;
						System.out.println("Enter an age between 0 and 150");
						break;
					}
				}
				
				
				if(!txtAge.getText().isEmpty() & valid == true)
				{
				age = Integer.parseInt(txtAge.getText());
				}
				else if(valid == true)
					{
					valid = false;
					System.out.println("Enter an age between 0 and 150.");
					}
				
				if(age > 150 || age < 0 & valid == true)
				{
					System.out.println("Enter an age between 0 and 150");
					valid = false;
				}
				
				
				double Salary = -1;
				
				testNumber  = txtSalary.getText().toCharArray();
				
				for(int i=0; i<testNumber.length; i++){
					if(Character.isLetter(testNumber[i]))
					{
						valid = false;
						System.out.println("Enter a valid salary");
						break;
					}
				}
				
				if(!txtSalary.getText().isEmpty() & valid == true)
				{
				Salary = Double.parseDouble(txtSalary.getText());
				}
				else if(valid == true)
				{
					valid = false;
					System.out.println("Enter a valid salary.");
				}
				if(Salary < 0 & valid == true)
				{
					System.out.println("Enter a valid salary");
					valid = false;
				}
				
				
				if(!txtSsn.getText().matches("\\d{3}-\\d{2}-\\d{4}"))
				{
					System.out.println("Enter a valid Social Security Number");
					valid = false;
				}

				if(txtEmployeeId.getText().isEmpty())
				{
					System.out.println("Enter an Employee ID");
					valid = false;
				}
				
				if(txtSupervisor.getText().isEmpty())
				{
					System.out.println("Enter a Supervisor.");
					valid = false;
				}
				
				
				if(valid)
				{
					Employee employee = new Employee(txtFirstName.getText(),
							txtLastName.getText(), Integer.parseInt(txtAge.getText()),
							txtSsn.getText(), Integer.parseInt(txtEmployeeId.getText()), 
							txtSupervisor.getText(), Double.parseDouble(txtSalary.getText()));
					
					employee.toString();
				}
				
				
				
				
				
			}
			
			
			
			
		}
		);
		
	}

	public Employee(String firstName, String lastName, int age, String ssn, int id, String supervisor, double salary) {
		super(firstName, lastName, age, ssn);
		// TODO Auto-generated constructor stub
		
		this.id = id;
		this.supervisor = supervisor;
		setSalary(salary);
	}

	public Employee()
	{
		// TODO Auto-generated constructor stub
	}

	/**
	 * @return the salary
	 */
	public double getSalary() {
		return salary;
	}

	/**
	 * @param salary the salary to set
	 */
	public void setSalary(double salary) {
		this.salary = salary;
	}
	
	
	
	
	
	
	
	@Override
	public String toString()
	{
		String studentString;
		studentString = "Employee Id: " + id + "\nFirst Name: " + firstName
				+ "\nLast Name: " + lastName + "\nSSN:" + getSsn() + "\nAge: "
				+ age + "\nSalary: " + getSalary() + "\nSupervisor: " + supervisor;
		System.out.println(studentString);
		return studentString;
	}
	

	
	

}
